<template>
  <div class="def">
    <img src="../../assets/login.jpg" alt="login">
    <div class="user">
    <h1>品尚服饰</h1>
    <div class="p">
        <p :class="{active:item}" @click="item=true">登录</p>
        <p :class="{active:!item}" @click="item=false">注册</p>
    </div>
    <!-- 登录 -->
    <div class="login" v-show="item"> 
        <div class="relative">
            <input type="text" placeholder="用户名" v-model="user">
            <i class="el-icon-user"></i><br/>
        </div>  
        <div class="relative">
            <input type="password" v-model="pass1" placeholder="密码">
            <i class="el-icon-key"></i>
        </div>
        <div class="relative">
            <input type="password" placeholder="确认密码" v-model="pass2">
            <i class="el-icon-circle-check"></i>
        </div>
        <button @click="login">登录</button>
    </div>
        <!-- 注册 -->
    <div class="signin" v-show="!item">
        <div class="relative">
            <input type="text" placeholder="用户名" v-model="user">
            <i class="el-icon-user"></i><br/>
        </div>  
        <div class="relative">
            <input type="password" placeholder="密码" v-model="pass1">
            <i class="el-icon-key"></i>
        </div>
        <button @click="register">注册</button>
    </div>
    </div>
  </div>
</template>

<script>
import {reqUserRegister,reqUserLogin} from '../../api'
export default {
    name:'Log',
    data() {
      return {
        item:true, //标识登录还是注册，true代表登录，false代表注册
        user:'', //用户名输入框绑定的值
        pass1:'', //密码输入框绑定的值
        pass2:''
      }
    },
    methods: {
      async register(){
        if (this.user === "" || this.pass1 === "") {
          this.$message.warning('请填写所有必填项！');
        }else{
          let Register = await reqUserRegister(this.user,this.pass1)
          if (Register.exist === true) {
            this.$message.warning('用户信息已存在，请直接登录！');
          }else{
            this.$message.success('注册成功！')
            // 将token保存在本地
            localStorage.setItem('key',JSON.stringify(Register))
            this.user = '';this.pass1='';this.pass2='';
            this.$router.back() //回退
          }
        }
      },
      async login(){
        if (this.user === "" || this.pass1 === "") {
          this.$message.warning("请填写所有必填项！")
        } else if (this.pass1 !== this.pass2){
          this.$message.warning("两次输入的密码不同！")
        }else{
          let Login = await reqUserLogin(this.user,this.pass1)
          if (Login.exist === false) {
            this.$message.warning('用户不存在，请注册用户！')
          }else{
            this.$message.success('登录成功！')
            localStorage.setItem('key',JSON.stringify(Login))
            this.user = '';this.pass1='';this.pass2='';
            this.$router.back() //回退
          }
        }
      }
    }
}
</script>

<style scoped>
    .active{
    color: #C69C6C;
    border-bottom: 1px solid #C69C6C;
  }
  .def{
    background: #f4f4f4;
    width: 100%;
    height: 90vh;
    position: relative;
    overflow: hidden;
    text-align: center;
  }
  .user{
    width: 400px;
    height: 400px;
    background: white;
    position: absolute;
    margin:60px auto;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
  }
  .relative{
    position: relative;
    width: 260px;
    margin: 0 auto;
  }
  .relative > i{
    position: absolute;
    left: 15px;
    margin-top: 12px;
  }
  h1{
    color: #C69C6C;
    margin: 20px 0 20px 0;
  }
  input{
    margin-bottom: 15px;
    background: #f4f4f4;
    width: 220px;
  }
  button{
    width: 250px;
    height: 40px;
    font-size: 16px;
    opacity: 1;
    color: black;
  }
  .p{
    width: 240px;
    padding: 10px;
    margin: 10px auto;
    overflow: hidden;
  }
  p{
    color: black;
    font-family:'微软雅黑';
    float: left;
    margin-left: 60px;
    cursor: pointer;
    
  }
  p:hover{
    color: #C69C6C;
    /* border-bottom: 2px solid #C69C6C; */
  }
</style>